package com.blb.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.blb.pojo.Department;
import com.blb.pojo.Employee;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface EmployeeMapper extends BaseMapper<Employee> {

    /**
     * 插入员工
     * @param employee
     */
    @Insert("insert into t_employee(name,age,gender,dept_id,position,image)" +
            " values(#{name},#{age},#{gender},#{deptId},#{position},#{image})")
    void insertEmployee(Employee employee);

    /**
     * 删除员工
     * @param id
     */
    @Delete("delete from t_employee where id = #{id}")
    void deleteEmployee(Long id);


    /**
     * 更新员工
     * @param employee
     */
    @Update("<script>" +
            "update t_employee\n" +
            "        <set>\n" +
            "            <if test=\"name != null and name != ''\">\n" +
            "                name = #{name},\n" +
            "            </if>\n" +
            "            <if test=\"age != null\">\n" +
            "                age = #{age},\n" +
            "            </if>\n" +
            "            <if test=\"gender != null and gender != ''\">\n" +
            "                gender = #{gender},\n" +
            "            </if>\n" +
            "            <if test=\"deptId != null\">\n" +
            "                dept_id = #{deptId},\n" +
            "            </if>\n" +
            "            <if test=\"position != null and position != ''\">\n" +
            "                position=#{position},\n" +
            "            </if>\n" +
            "            <if test=\"image != null and image != ''\">\n" +
            "                image=#{image}\n" +
            "            </if>\n" +
            "        </set>\n" +
            "        where id = #{id}" +
            "</script>")
    void updateEmployee(Employee employee);

    /**
     * 关联查询配置
     * @return
     */
    @Results(id = "employeeMap2",value = {
//            @Result(id = true,property = "id",column = "id"),
//            @Result(property = "name",column = "name"),
            @Result(property = "deptId",column = "dept_id"),
            //配置部门管理查询
            @Result(property = "department",javaType = Department.class,column = "dept_id",
                    one = @One(select = "com.blb.mapper.DepartmentMapper.selectById"))
    })
    @Select("select * from t_employee")
    List<Employee> selectAll();

    /**
     * 通过姓名进行模糊查询
     * @param name
     * @return
     */
    @ResultMap("employeeMap2")
    @Select("select * from t_employee where name like concat('%',#{name},'%')")
    List<Employee> selectEmployeesLikeName(String name);

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @Results(id = "employeeMap",value = {
            @Result(property = "id",column = "e_id"),
            @Result(property = "name",column = "e_name"),
            @Result(property = "deptId",column = "dept_id"),
            @Result(property = "age",column = "age"),
            @Result(property = "position",column = "position"),
            @Result(property = "image",column = "image")
    })
    @Select("select e.id e_id,e.name e_name,e.age,e.gender,e.position,e.image,e.dept_id,\\n\" +\n" +
            "            \"  d.id d_id,d.name d_name,d.address\\n\" +\n" +
            "            \"  from t_employee e join t_department d\\n\" +\n" +
            "            \"  on e.dept_id = d.id " +
            "where e.id = #{id}")
    Employee selectEmployeeById(Long id);


    /**
     * 通过年龄区间查询员工
     * @param min
     * @param max
     * @return
     */
    @ResultMap("employeeMap2")
    @Select("select * from t_employee where age between #{min} and #{max}")
    List<Employee> selectEmployeeByAge(@Param("min") Long min,
                                       @Param("max") Long max);

    /**
     * 通过员工的多个属性查询员工列表
     * @param employee
     * @return
     */
    @ResultMap("employeeMap2")
    @Select("<script>        " +
            "select * from t_employee\n" +
            "        <where>\n" +
            "            <if test=\"name != null and name != ''\">\n" +
            "                name like concat('%',#{name},'%')\n" +
            "            </if>\n" +
            "            <if test=\"age != null\">\n" +
            "                and age = #{age}\n" +
            "            </if>\n" +
            "            <if test=\"gender != null and gender != ''\">\n" +
            "                and gender = #{gender}\n" +
            "            </if>\n" +
            "            <if test=\"deptId != null\">\n" +
            "                and dept_id = #{deptId}\n" +
            "            </if>\n" +
            "            <if test=\"position != null and position != ''\">\n" +
            "                and position = #{position}\n" +
            "            </if>\n" +
            "        </where>" +
            "</script>")
    List<Employee> selectEmployeesByEmployee(Employee employee);

    /**
     * 按多个id查询员工
     * @param ids
     * @return
     */
    @ResultMap("employeeMap2")
    @Select("<script>" +
            "select * from t_employee\n" +
            "        where id in\n" +
            "        <foreach collection=\"ids\" item=\"id\" open=\"(\" close=\")\" separator=\",\">\n" +
            "            #{id}\n" +
            "        </foreach>" +
            "</script>")
    List<Employee> selectEmployeesByIds(@Param("ids") List<Long> ids);

    /**
     * 通过部门id查员工
     * @param deptId
     * @return
     */
    @Select("select * from t_employee where dept_id = #{deptId}")
    List<Employee> selectEmployeesByDeptId(Long deptId);
}
